const { Service } = require("egg");

class sectionService extends Service {
    //添加节
    async addSection(body) {
            try {
                const section = {
                    title: body.title,
                    orderby: body.orderby,
                    md_text: body.md_text,
                    html_text: body.html_text,
                    chapter_id: body.chapter_id,
                }
                await this.app.model.Section.create(section)
                return true
            } catch (error) {

            }
        }
        //获取节
    async getSection(chapter_id) {
            try {
                const SectionList = await this.app.model.Section.findAll({
                    order: [
                        ['orderby', "asc"]
                    ],
                    include: [{
                        model: this.app.model.Chapter,
                        as: "chapter"
                    }],
                    where: {
                        chapter_id
                    }
                })
                return SectionList
            } catch (error) {
                return null
            }
        }
        //删除节
    async deleteSection(id) {
            try {
                await this.app.model.Section.destroy({
                    where: {
                        id
                    }
                })
                return true
            } catch (error) {
                return false
            }
        }
        //更新节
    async updateSection(id, body) {
        try {
            const section = {
                title: body.title,
                orderby: body.orderby,
                md_text: body.md_text,
                html_text: body.html_text,
                chapter_id: body.chapter_id,
            }
            await this.app.model.Section.update(section, {
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false
        }
    }
    async getSectionList(chapterList){
            for(let item of chapterList){
                let chapter_id = item.id
                const sections = await this.app.model.Section.findAll({
                    where:{
                        chapter_id
                    }
                })
                item.dataValues.sectionList = sections
            }
            return chapterList

    }

    async getSectionDetail(section_id){
       const sectionDetail = await this.app.model.Section.findOne({
            where:{
                id:section_id
            }
        })
        return sectionDetail
    }

    async getSectionsMenu(section_id){
        const section = await this.app.model.Section.findOne({
            where:{
                id:section_id
            },
            include:{
                model:this.app.model.Chapter,
                as : "chapter",
                include:{
                    model:this.app.model.Book,
                    as : "book"
                }
            }
        })
        const book_id = section.dataValues.chapter.dataValues.book.id
        const chapters = await this.app.model.Chapter.findAll({
            include:{
                model:this.app.model.Book,
                as:"book"
            },
            where:{
              book_id
            }
        })

        for(let item of chapters){
            let chapter_id = item.id
            const sections = await this.app.model.Section.findAll({
                where:{
                    chapter_id
                }
            })
            item.dataValues.sectionList = sections
        }
        return chapters
    }



}

module.exports = sectionService